<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>$GODOT_PROJECT_NAME</title>
    <style>
        /* 全局样式 */
        body {
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
            font-family: 'Arial', 'Microsoft YaHei', sans-serif;
            overflow: hidden;
            width: 100vw;
            height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        /* 画布样式 */
        #canvas {
            display: block;
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            z-index: 10;
        }

        /* 加载界面样式 */
        #loading-screen {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: #2c3e50;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 20;
            transition: opacity 0.5s ease;
        }

        #loading-logo {
            width: 200px;
            height: 200px;
            margin-bottom: 30px;
            background-image: url('$GODOT_SPLASH');
            background-size: contain;
            background-position: center;
            background-repeat: no-repeat;
        }

        #loading-progress-container {
            width: 300px;
            height: 20px;
            background-color: rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            overflow: hidden;
            margin-bottom: 20px;
        }

        #loading-progress {
            width: 0%;
            height: 100%;
            background-color: #27ae60;
            border-radius: 10px;
            transition: width 0.3s ease;
        }

        #loading-status {
            color: white;
            font-size: 16px;
            margin-top: 10px;
            text-align: center;
            min-height: 20px;
        }

        /* 错误提示样式 */
        #error-display {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.8);
            z-index: 30;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: white;
            padding: 20px;
            box-sizing: border-box;
            text-align: center;
        }

        #error-title {
            font-size: 24px;
            margin-bottom: 20px;
            color: #e74c3c;
        }

        #error-message {
            font-size: 16px;
            margin-bottom: 30px;
            max-width: 600px;
        }

        #retry-button {
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        #retry-button:hover {
            background-color: #2980b9;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            #loading-logo {
                width: 150px;
                height: 150px;
            }

            #loading-progress-container {
                width: 250px;
            }
        }

        @media (max-width: 480px) {
            #loading-logo {
                width: 120px;
                height: 120px;
            }

            #loading-progress-container {
                width: 200px;
            }
        }
    </style>
    $GODOT_HEAD_INCLUDE
</head>
<body>
    <!-- 程序画布 -->
    <canvas id="canvas"></canvas>

    <!-- 加载界面 -->
    <div id="loading-screen">
        <div id="loading-logo" class="$GODOT_SPLASH_CLASSES"></div>
        <div id="loading-progress-container">
            <div id="loading-progress"></div>
        </div>
        <div id="loading-status">正在加载程序资源...</div>
    </div>

    <!-- 错误提示 -->
    <div id="error-display">
        <div id="error-title">加载出错</div>
        <div id="error-message">程序加载过程中发生错误，请检查网络连接后重试。</div>
        <button id="retry-button">重新加载</button>
    </div>

    <!-- Godot引擎脚本 -->
    <script src="$GODOT_URL"></script>
    <script>
        // 配置对象
        const GODOT_CONFIG = $GODOT_CONFIG;

        // DOM元素引用
        const loadingScreen = document.getElementById('loading-screen');
        const loadingProgress = document.getElementById('loading-progress');
        const loadingStatus = document.getElementById('loading-status');
        const errorDisplay = document.getElementById('error-display');
        const errorMessage = document.getElementById('error-message');
        const retryButton = document.getElementById('retry-button');
        const canvas = document.getElementById('canvas');

        // 重试按钮事件
        retryButton.addEventListener('click', function() {
            errorDisplay.style.display = 'none';
            startGame();
        });

        // 更新加载进度
        function updateProgress(current, total) {
            if (total > 0) {
                const percent = Math.min(100, Math.round(current / total * 100));
                loadingProgress.style.width = percent + '%';
                loadingStatus.textContent = `加载中... ${percent}%`;
            } else {
                loadingStatus.textContent = '正在准备程序环境...';
            }
        }

        // 显示错误
        function showError(message) {
            errorMessage.textContent = message || '程序加载过程中发生错误，请检查网络连接后重试。';
            errorDisplay.style.display = 'flex';
        }

        // 隐藏加载界面
        function hideLoadingScreen() {
            loadingScreen.style.opacity = '0';
            setTimeout(() => {
                loadingScreen.style.display = 'none';
            }, 500);
        }

        // 启动程序
        function startGame() {
            try {
                // 创建引擎实例
                const engine = new Engine(GODOT_CONFIG);

                // 设置进度回调
                engine.startGame({
                    canvas: canvas,
                    onProgress: updateProgress,
                    onPrint: (text) => console.log('[Godot]', text),
                    onPrintError: (text) => console.error('[Godot Error]', text)
                }).then(() => {
                    console.log('程序启动成功！');
                    hideLoadingScreen();
                }).catch((error) => {
                    console.error('程序启动失败:', error);
                    showError(error.message);
                });
            } catch (error) {
                console.error('引擎初始化失败:', error);
                showError(error.message);
            }
        }

        // 检测WebGL支持
        function checkWebGLSupport() {
            if (!Engine.isWebGLAvailable()) {
                showError('您的浏览器不支持WebGL，无法运行程序。请尝试使用Chrome、Firefox或Edge浏览器。');
                return false;
            }
            return true;
        }

        // 页面加载完成后启动程序
        window.addEventListener('load', function() {
            if (checkWebGLSupport()) {
                startGame();
            }
        });

        // 处理窗口大小变化
        window.addEventListener('resize', function() {
            // 可以在这里添加额外的响应式处理逻辑
        });
    </script>
</body>
</html>
